#include "Socket.hpp"
#include "Protocol.hpp"


void Warn(char *in)
{
    std::cout<<"User port"<<*in<<" serverid ,serverport "<<std::endl;
}

int main(int argc,char *argv[])
{
    if(argc != 3)
    {
        Warn(argv[0]);
    }
    Socket client_so;
    std::string serverip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    client_so.Getsocket();
    bool ret = client_so.Getconnect(serverip,port);
    if(!ret)return 1;

    srand(time(nullptr));
    
    std::string op_str = "+-*/%^";

    int cnt = 5;
    while(cnt--)
    {
        int x = rand()%100+1;
        usleep(1020);
        int y = rand()%100;
        usleep(4500);

        char op = op_str[rand()%op_str.size()];

        Request res(x,y,op);
        std::string temp;
        res.Serialization(&temp);

        temp = Encode(temp);
        write(client_so.Ret_fd(),temp.c_str(),temp.size());

        std::string stream_buffer;
        char buffer[128];
        ssize_t n = read(client_so.Ret_fd(), buffer, sizeof(buffer)); // 我们也无法保证我们能读到一个完整的报文
        if(n > 0)
        {
            buffer[n] = 0;
            stream_buffer += buffer; // "len"\n"result code"\n

            std::cout << stream_buffer << std::endl;
        }
        sleep(1);
    }
    client_so.Close();
    return 0 ;  
}